import React from 'react'
import { HashRouter as Router, Route, Switch } from "react-router-dom"
import { createStore } from 'redux'
import { Provider } from 'react-redux'

import LazyLoad from './pages/components/common/LazyLoad'
import reducer from './redux/store.js'
import middleware from './utils/middleware.js'

let store = createStore(reducer, middleware)

const files = require.context('./pages', true, /route\.js$/)
let routers = files.keys().reduce((routers, route) => {
  let router = files(route).default
  return routers.concat(router)
}, [])


export default () => {

  return (
    <Provider store={store}>
      <Router>
        <Switch>
          {routers.map((h, index) => {
            return <Route exact={h.exact} path={h.path} render={() => <LazyLoad Loading={h.component} />} key={index} />
          })}
        </Switch>
      </Router>
    </Provider>
  )
}